<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
		    <input type="hidden" id="id" name="id">
			<div class='form-group'>
				<label class='col-md-4 control-label'>分类名称</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='分类名称' type='text' name='name' id='name' data-bv-notempty='true' data-bv-notempty-message='分类名称 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>上级商品类型</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='parentId' id='parentId'>
					<input class='form-control' type='hidden' name='parentIds' id='parentIds'>
					<input style="width: 90%; float: left" class='form-control' type='text'
						   name='parentName' id='parentName' placeholder='上级商品类型'
						   data-bv-notempty='true' data-bv-notempty-message='上级商品类型 不能为空' readonly >
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTree('parentId', 'parentName', 'goodsCategory', '商品类型', 'root', 'select')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否可用</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="usable" id="usable"
							title="是否可用" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>排序</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='排序' type='text' name='sort' id='sort' data-bv-notempty='true' data-bv-notempty-message='排序 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>备注</label>
				<div class='col-md-8'>
					<textarea class="form-control" placeholder="备注" name="remark" id="remark"></textarea>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>
		</fieldset>
	</form>
</div>
	<script type="text/javascript" src="/js/jq.js"></script>
	<script type="text/javascript" src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="/js/common.js"></script>
	<script type="text/javascript" src="/layui/layui.js"></script>
	<script type="text/javascript" src="/js/dict.js"></script>
	<script type="text/javascript" src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript" src="/js/my/ztree-menu.js"></script>
	<script type="text/javascript">
        $(function () {
            $('#form').bootstrapValidator();
        });

		layui.use(['layer','laydate'], function(){
		    var layer = layui.layer;
		});

        showDictSelect('usable', 'boolean', '', '1');

        var parentId = getUrlParam('parentId') ? getUrlParam('parentId') : 0;
        var parentName = getUrlParam('parentName') ? getUrlParam('parentName') : 'root';
        var isSelected = false; // 树结构是否选择

        $('#parentId').val(parentId);
        $('#parentName').val(parentName);

		initData();
		
		function initData(){
			var id = getUrlParam("id");
			if(id){
				$.ajax({
					type : 'get',
					url : '/goodsCategory/'+id,
					async : false,
					success : function(json) {
					    if (json.code === 0) {
                            var data = json.data;
							$('#id').val(data.id);
							$('#name').val(data.name);
							$('#parentId').val(data.parentId);
							$('#parentName').val(data.parentName);
							$('#parentIds').val(data.parentIds);
							$('#usable').val(data.usable);
							$('#remark').val(data.remark);
							// $('#createTime').val(data.createTime);
							// $('#updateTime').val(data.updateTime);
							// $('#createBy').val(data.createBy);
							// $('#updateBy').val(data.updateBy);
							// $('#isDeleted').val(data.isDeleted);
							$('#sort').val(data.sort);

                        }
					}
				});
			}
		}

		function save() {
			$('#form').bootstrapValidator();
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
		    if(!bootstrapValidator.isValid()){
			   return;
		    }
		    
		    var formdata = $("#form").serializeObject();

			$.ajax({
				type : 'put',
				url : '/goodsCategory',
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(json) {
                    if (json.code === 0) {
                        layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                            parent.location.reload();//关闭iFrame
                        });
                    } else {
                        layer.msg("修改失败：" + json.message, {shift: -1, time: 1000});
                    }
				}
			});
		}
	</script>
</body>
</html>
